package recipe.renderer;

import android.app.Application;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

// http://developer.android.com/design/style/metrics-grids.html
// http://developer.android.com/reference/android/app/ListActivity.html
// http://www.vogella.de/articles/AndroidListView/article.html#overview_listview
public class RecipesList extends ListActivity {
	private Recipe[] availableRecipes = null;
	private static final int MENU_SETTINGS = Menu.FIRST; 
	private static final int MENU_ABOUT    = Menu.FIRST + 1; 
	private static final int MENU_QUIT     = Menu.FIRST + 2; 
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		availableRecipes = loadDefaultRecipes();
        
        //ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getCurrentlyAvailableRecipesNames());
        RecipeListAdapter adapter = new RecipeListAdapter(this,R.layout.recipe_list_row_layout,availableRecipes);
        setListAdapter(adapter);
    }
    
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Recipe clickedRecipe = availableRecipes[position];
		
		Toast.makeText(this, "#" + (id+1) + " - " + clickedRecipe.title + " selected", Toast.LENGTH_LONG).show();
		Intent intent = new Intent(this, Recipe_rendererActivity.class);
		intent.putExtra("recipe_resource", recipe_resources[(int) id]);
		this.startActivity(intent);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		/*
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.recipelistmenu, menu);
	    */
		
		menu.add(0, MENU_SETTINGS, 0, "Settings");
		menu.add(0, MENU_ABOUT   , 0, "About");
		menu.add(0, MENU_QUIT    , 0, "Quit");
	    return true;
	}	
	
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	//@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case MENU_SETTINGS:
	        	Toast.makeText(this, "Settings", Toast.LENGTH_SHORT);
	            return !true;
	        case MENU_ABOUT:
	        	Toast.makeText(this, "Recipe player WW8", Toast.LENGTH_LONG);
	        	return true;
	        case MENU_QUIT:
	        	finish();
	        	return true;
	        default:
	        	Toast.makeText(this, "CRAP", Toast.LENGTH_SHORT);
	            return super.onOptionsItemSelected(item);
	    }
	}

	private String[] getCurrentlyAvailableRecipesNames() {
		String [] names = new String[availableRecipes.length];
		if (availableRecipes==null)
			return names;
		for ( int i=0; i<availableRecipes.length; i++ ) {
			Recipe recipe = availableRecipes[i];
			if (recipe!=null)
				names[i] = recipe.title;
			else
				names[i] = "NA";
		}
		return names;
	}
	
	private int[] recipe_resources = { 
			R.raw.healthy_baked_chicken_nuggets,
			R.raw.dummy_recipe_1, 
			R.raw.dummy_recipe_2,
			R.raw.dummy_recipe_3,
			R.raw.dummy_recipe_4
	};
	private Recipe[] loadDefaultRecipes() {
		Recipe[] recipes = new Recipe[recipe_resources.length];
        for (int index=0; index<recipe_resources.length; index++) { 
        	recipes[index] = new Recipe();
        	recipes[index].parse_file(getResources().openRawResource(recipe_resources[index]));
        }
        
        return recipes;
	}
}